package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.UserRoleDTO;
import top.zywork.query.UserRoleQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.UserRoleService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.UserRoleVO;

import java.util.List;

/**
 * UserRoleController控制器类<br/>
 *
 * 创建于2019-06-27<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/user-role")
@Slf4j
public class UserRoleController extends BaseController {

    private UserRoleService userRoleService;

    /**
     * 批量保存用户与角色配置信息
     *
     * @param userRoleVOList
     * @param bindingResult
     * @return
     */
    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存用户角色关联")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<UserRoleVO> userRoleVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        userRoleService.saveBatch(BeanUtils.copyListObj(userRoleVOList, UserRoleDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @PostMapping("admin/remove-role/{userId}")
    @SysLog(description = "后台删除指定用户的多个角色")
    public ResponseStatusVO removeUserRole(@PathVariable("userId") Long userId, @RequestBody List<Long> roleIdList) {
        userRoleService.removeUserRole(userId, roleIdList);
        return ResponseStatusVO.ok("删除用户角色成功", null);
    }

    @GetMapping("admin/multi/{id}")
    public ResponseStatusVO listById(@PathVariable("id") Long id) {
        PagerDTO pagerDTO = userRoleService.listById(id);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserRoleVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 查询登录用户自己的所有角色
     *
     * @return
     */
    @GetMapping("user/list")
    public ResponseStatusVO listUserRoles() {
        return listById(SecurityUtils.getJwtUser().getUserId());
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = userRoleService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserRoleVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody UserRoleQuery userRoleQuery) {
        PagerDTO pagerDTO = userRoleService.listAllByCondition(userRoleQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserRoleVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody UserRoleQuery userRoleQuery) {
        PagerDTO pagerDTO = userRoleService.listPageByCondition(userRoleQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), UserRoleVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @Autowired
    public void setUserRoleService(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }
}
